﻿package com.tvein.sanguo.swc.action.room
{
	//flash
	import flash.media.Sound;
	import flash.media.SoundTransform;
	import flash.media.SoundChannel;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextFormat;
	import flash.filters.ColorMatrixFilter
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Room2 extends MovieClip
	{
		public static const NAME:String = "Room-2";
		
		[Embed(source = '../source/sound/Room/2.mp3', mimeType='audio/mpeg')] static private const MP3Effect:Class;
		private var mp3Effect:Sound = new MP3Effect();
		private var channel:SoundChannel = new SoundChannel();
		private var soundT:SoundTransform = new SoundTransform();
		
		private var sourceServer:String;
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var roomName:String;
		private var intWinner:int;
		private var intLoser:int;
		private var strWinner:String;
		private var strLoser:String;
		
		private var objLog:Object;
		
		public function Room2()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			mcFrame.reSize(600, 450);
			
			var square:Shape = new Shape();
			square.graphics.beginFill(0x333333, 1);
			square.graphics.drawRect(30, 40, 540, 290);
			square.graphics.beginFill(0xC10E1F, 1);
			square.graphics.drawRect(4, 330, 592, 20);
			square.graphics.endFill();
			this.addChild(square);
			
			btnReplay.addEventListener(MouseEvent.CLICK, onClick);
			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setSource":
					sourceServer = params.Address;
					break;
				case "setEffect":
					effectVol = params.Volume;
					soundT.volume = effectVol * .01;
					channel = mp3Effect.play(0, 1, soundT);
					break;
				case "setRoom":
					roomName = String(params);
					break;
				case "setData":
					setData(params);
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			for (var i:int = 0; i < params.length; i++)
			{
				switch(params[i].Type)
				{
					case 1:		//Winner
						intWinner = params[i].RoleID;
						strWinner = params[i].RoleName;
						
						var winnerLoader:Loader = new Loader();
						winnerLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onWinnerLoad);
						winnerLoader.load(new URLRequest(sourceServer + "/SanGuo/role/head/" + params[i].ImageID + ".png"));
						
						break;
					case 2:		//Loser
						intLoser = params[i].RoleID;
						strLoser = params[i].RoleName;
						
						var loserLoader:Loader = new Loader();
						loserLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onLoserLoad);
						loserLoader.load(new URLRequest(sourceServer + "/SanGuo/role/head/" + params[i].ImageID + ".png"));
						
						break;
					case 3:		//Log
						objLog = params[i].Log;
						break;
				}
			}
		}
		
		private function _onWinnerLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, _onWinnerLoad);
			
			if (intWinner == 1)
			{
				evt.target.content.x = 50;
				evt.target.content.y = 60;
				mcWin.x = 50;
				mcWin.y = 220;
				txtName1.text = strWinner;
			}
			else
			{
				evt.target.content.x = 310;
				evt.target.content.y = 60;
				mcWin.x = 462;
				mcWin.y = 220;
				txtName2.text = strWinner;
			}
			
			this.addChild(evt.target.content);
			
			this.setChildIndex(txtName1, this.numChildren - 1);
			this.setChildIndex(txtName2, this.numChildren - 1);
			this.setChildIndex(mcVC, this.numChildren - 1);
			this.setChildIndex(mcWin, this.numChildren - 1);
			this.setChildIndex(mcLose, this.numChildren - 1);
		}
		
		private function _onLoserLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, _onLoserLoad);
			
			if (intLoser == 1)
			{
				evt.target.content.x = 50;
				evt.target.content.y = 60;
				mcLose.x = 50;
				mcLose.y = 220;
				txtName1.text = strLoser;
			}
			else
			{
				evt.target.content.x = 310;
				evt.target.content.y = 60;
				mcLose.x = 462;
				mcLose.y = 220;
				txtName2.text = strLoser;
			}
			
			this.addChild(evt.target.content);
			
			var matrix:Array = new Array();
			matrix = matrix.concat([0.3086, 0.6094, 0.0820, 0, 0]); // red
 			matrix = matrix.concat([0.3086, 0.6094,0.0820, 0, 0]); // green
 			matrix = matrix.concat([0.3086, 0.6094, 0.0820, 0, 0]); // blue
 			matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha

			var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
			var filters:Array = new Array();
 			filters.push(filter);
			
 			evt.target.content.filters = filters;

			this.setChildIndex(txtName1, this.numChildren - 1);
			this.setChildIndex(txtName2, this.numChildren - 1);
			this.setChildIndex(mcVC, this.numChildren - 1);
			this.setChildIndex(mcWin, this.numChildren - 1);
			this.setChildIndex(mcLose, this.numChildren - 1);
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol *.01);
			
			switch(evt.target.name)
			{
				case "btnReplay":
					dispatchEvent(new SwcEvent(NAME, ["-replayLog", [roomName, objLog]]));
					break;
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["RoomCall(1,RoomBack,,);"]]));
					break;
			}
			
			dispatchEvent(new SwcEvent(NAME, ["-close", "Room-2"]));
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnReplay.removeEventListener(MouseEvent.CLICK, onClick);
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}